<template>
	<view class="orders" :class="[this.roleClass]">
		<picker mode="date"  @change="changeDate">
			<view class="month-picker">
				<view class="month-value">{{date[0]}}年{{date[1]}}月{{date[2]}}日</view>
				<view class="arrow-down"></view>
			</view>
		</picker>
		<view class="stats">
			<view class="stats-item" v-for="(item,index) in stats" :key="index">
				<view class="stats-title">{{item.title}}</view>
				<view class="stats-value">{{statsData[item.index]}}</view>
			</view>
		</view>
		<!-- 列表 -->
		<mescroll-uni class="list-wrap" @getData="getListData" :top="mescollTop" refs="mescroll" :size="10">
			<block slot="list">
				<view class="list" v-if="list.length > 0">
					<view class="member-item" v-for="item in list" :key="item.id">
						<view class="member-head">
							<view class="flex-left">
								<view class="member-icon"></view>
								<view class="member-name">{{item.site_name || item.fenxiao_name}}</view>
							</view>
							<view class="member-link" @click="navigateToMore(item.site_id || item.fenxiao_id)">
								<view class="link-text">查看更多</view>
								<view class="link-icon"></view>
							</view>
						</view>
						<view class="member-body" v-if="role == 'merchant'">
							<view class="member-itmes">
								<view class="member-data">
									<view class="data-title">线上订单量</view>
									<view class="data-value">{{item.onlin_order_num}}</view>
								</view>
								<view class="member-data">
									<view class="data-title">线上订单金额</view>
									<view class="data-value">¥{{item.onlin_total_money}}</view>
								</view>
							</view>
							<view class="member-itmes">
								<view class="member-data">
									<view class="data-title">线下订单量</view>
									<view class="data-value">{{item.offline_order_num}}</view>
								</view>
								<view class="member-data">
									<view class="data-title">线下订单金额</view>
									<view class="data-value">¥{{item.offline_total_money}}</view>
								</view>
							</view>
						</view>
						<view class="member-body" v-if="role == 'promoters'">
							<view class="member-itmes">
								<view class="member-data">
									<view class="data-title">订单量</view>
									<view class="data-value">{{item.fenxiao_order_count}}</view>
								</view>
								<view class="member-data">
									<view class="data-title">订单金额</view>
									<view class="data-value">¥{{item.fenxiao_money_sum}}</view>
								</view>
							</view>
						</view>
					</view>
				</view>
				<ns-empty v-else text="暂无记录" :fixed="!1" :isIndex="!1"></ns-empty>
			</block>
		</mescroll-uni>
		<loading-cover ref="loadingCover"></loading-cover>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				role: '',
				roleClass: '',
				date:[],
				stats:[],
				promotersStats: [{
					title: '线上订单数量',
					index: 'fenxiao_order_count'
				}, {
					title: '线上订单金额(元)',
					index: 'fenxiao_money_sum'
				}],
				merchantStats: [{
					title: '线上订单数量',
					index: 'onlineCount'
				}, {
					title: '线上订单金额(元)',
					index: 'onlineMoneySum'
				},{
					title: '线下订单数量',
					index: 'offlineCount'
				}, {
					title: '线下订单金额(元)',
					index: 'offlineMoneySum'
				}],
				statsData: {},
				list: [],
				page: 1,
				page_size: 10,
				mescollTop: 400
			};
		},
		computed: {

		},
		onLoad(options) {
			this.role = options.role;
			let title = '商家订单';
			this.stats = this.merchantStats;

			if (this.role === 'teamleader') {
				title = '团长订单';
				this.roleClass = 'teamleader';
			} else if (this.role === 'promoters') {
				title = '店主订单';
				this.roleClass = 'promoters';
				this.stats = this.promotersStats;
				this.mescollTop = 300;
			}
			uni.setNavigationBarTitle({
				title
			});
			// 初始化时间
			let today = new Date();
			this.date = [today.getFullYear(),today.getMonth()+1,today.getDate()];
			this.getShopHead();
		},
		methods:{
			navigateToMore(id){
				let url = `./orders-data?role=${this.role}&id=${id}`;
				uni.navigateTo({url})
			},
			changeDate(e){
				this.date = e.detail.value.split('-');
				this.list = [];
				this.getShopHead();
				this.mescroll.resetUpScroll();
			},
			getShopHead() {
				let url = '/city/api/index/shopHead';
				if (this.role === 'promoters') {
					url = '/city/api/index/promoterHead';
				}
				this.$api.sendRequest({
					url: url,
					data: {
						date: this.date.join('-')
					},
					success: res => {
						this.statsData = res.data;
					}
				});
			},
			getListData(mescroll) {
				let url = '/city/api/index/shopOrder';
				if (this.role === 'promoters') {
					url = '/city/api/index/promoterOrder';
				}
				this.mescroll = mescroll;
				this.$api.sendRequest({
					url: url,
					data: {
						page: mescroll.num,
						page_size: mescroll.size,
						date: this.date.join('-')
					},
					success: res => {
						let newArr = [];
						if (res.code == 0 && res.data) {
							newArr = res.data.list;
						} else {
							this.$util.showToast({
								title: res.message
							});
						}
						
						mescroll.endSuccess(newArr.length);
						//设置列表数据
						if (mescroll.num == 1) this.list = []; //如果是第一页需手动制空列表
						
						this.list = this.list.concat(newArr); //追加新数据						
						if (this.$refs.loadingCover) this.$refs.loadingCover.hide();
					}
				});
			}
		}
	}
</script>

<style lang="scss" scoped>
	@import '../common/css/mixins.scss';
	$imgBusinessPath:'business/';

	@mixin box {
		width: 690rpx;
		background-color: #fff;
		border-radius: 8rpx;
	}

	.orders {
		padding: 30rpx;
		min-height: 580rpx;
		background-image: linear-gradient(180deg, #FFEADB 0%, #F8F8F8 100%);
		background-size: 100% 580rpx;
		background-repeat: no-repeat;

		&.teamleader {
			background-image: linear-gradient(180deg, #FFEDED 0%, #F8F8F8 100%);
		}

		&.promoters {
			background-image: linear-gradient(180deg, #E5FAF6 0%, #F8F8F8 100%);
		}
	}

	.month-picker {
		@include flex(space-between);
		@include box;
		padding: 0 50rpx;
		height: 88rpx;

		.month-value {
			font-size: 32rpx;
			color: #303133;
			line-height: 45rpx;
		}

		.arrow-down {
			@include image('arrow-fill-down.png');
			width: 28rpx;
			height: 28rpx;
		}
	}

	.stats {
		// @include flex(space-between);
		margin-top: 24rpx;
		@include box;
		padding: 30rpx;
		overflow: hidden;
		// height: 240rpx;

		.stats-item {
			width: 50%;
			@include flex;
			float: left;
			flex-direction: column;

			.stats-title {
				font-size: 24rpx;
				color: #909399;
				line-height: 33rpx;
			}

			.stats-value {
				font-size: 48rpx;
				font-family: PingFangSC-Medium, PingFang SC;
				font-weight: 500;
				color: #FF4544;
				line-height: 67rpx;
			}
		}
	}

	.flex-left {
		@include flex(flex-start);
	}

	.list {
		padding: 0 30rpx;
		.member-item {
			@include box;
			margin-top: 24rpx;

			.member-head {
				@include flex(space-between);
				padding: 30rpx;
				border-bottom: 1rpx solid #ebebeb;

				.member-icon {
					width: 40rpx;
					height: 40rpx;
					@include image('icon-store.png');
				}

				.member-name {
					font-size: 28rpx;
					color: #303133;
					line-height: 40rpx;
				}
			}

			.member-link {
				@include flex;
				.link-text {
					font-size: 24rpx;
					color: #303133;
					line-height: 46rpx;
				}

				.link-icon {
					@include image('icon-link-arrow.png');
					width: 32rpx;
					height: 32rpx;
				}
			}

			.member-body {
				padding: 30rpx;
				.member-itmes {
					@include flex(space-between);
					padding-bottom: 20rpx;
					.member-data {
						@include flex(flex-start);
						.data-title,.data-value {
							font-size: 28rpx;
							color: #909399;
							line-height: 34rpx;
						}
					
						.data-value {
							margin-left: 4rpx;
							color: #303133;
						}
					}
				}
			}
		}



	}
</style>
